
/**
 * Project Name:cats-core-impl
 * File Name:ResourceDao.java
 * Package Name:com.catsic.core.resource.dao
 * Date:2014年3月24日下午3:35:53
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.resource.dao;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;

import com.catsic.base.BaseDao;
import com.catsic.core.entity.Resource;


/**
 * ClassName: ResourceDao
 * Description: 资源Dao
 * date: 2014年3月24日 下午3:35:53
 * author: yangyd
 */
public interface ResourceDao extends BaseDao<Resource, String> {
	
	/**
	 * findByName:通过名称获得资源列表.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	public List<Resource> findByName(String name);
	
	@Query("select r from Resource r order by r.menu.sort asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public Resource findAllBySort(String code);
	
	/**
	 * findByCode:通过CODE获得资源列表.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	//@Query("select r from Resource r where r.code = ?1")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public Resource findByCode(String code);
	
	/**
	 * findByLikeCode:通过CODE获得资源列表.
	 * @author yangyd
	 * @param name
	 * @return
	 */
	@Query("select r from Resource r where r.code like ?1 order by r.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Resource> findByLikeCode(String code);
	
	/**
	 * findByUserIdOrderByCodeDesc:用户拥有的权限
	 * @author yangyd
	 * @param name
	 * @return
	 */
	@Query("select distinct rs from User u join u.roles r join r.resources rs where u.id = ?1 order by rs.menu.sort asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Resource> findByUserIdOrderByCodeAsc(String id);
	
	/**
	 * findByCodeIsNotNullAndParentIdOrderByCodeDesc:查询父节点下所有编码不为空的子节点,按编码倒叙排列
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	@Query("select r from Resource r where r.menu.code = ?1 order by r.code desc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Resource> findByCodeIsNotNullAndMenuCodeOrderByCodeDesc(String code);
	
	/**
	 * 通过父节点查询所有子节点,按code升序排列
	 * @param id
	 * @return	
	 */
	@Query("select r from Resource r where r.menu.code = ?1 order by r.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Resource> findByMenuCode(String menuCode);
	
	/**
	 * 通过userId 获取资源
	 * @param id
	 * @return
	 */
	@Query("select distinct rs from User u join u.roles r join r.resources rs where u.id = ?1 and rs.menu.code = ?2 and rs.drawer.code = ?3 and rs.authArea.code = ?4 order by rs.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Resource> findResourcesByUserIdOrderByCodeAsc(String userId,String menuCode,String drawerCode,String authAreaCode);
}
